package cn.lr.basic.controller;

import cn.lr.basic.Exception.BusinessException;
import cn.lr.basic.dto.LoginDto;
import cn.lr.basic.service.ILoginService;
import cn.lr.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;
    @RequestMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> account = loginService.account(dto);
            return AjaxResult.me().setResultObj(account);
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    @RequestMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> param){
        try {
            return loginService.wechat(param);
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
    @RequestMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody Map<String,String> param){
        try {
            Map<String, Object> map = loginService.binder(param);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
}
